library(dplyr)
library(tidyr)
library(ggplot2)

hyr_data <- read.csv("hyperraising data clean.csv", header = TRUE)

# Creates score labels
score_labels <- c("5" = "Acceptable",
                  "4" = "Almost Acc.",
                  "3" = "Neutral", 
                  "2" = "Almost Unacc.", 
                  "1" = "Unacceptable")


# Creates stacked bar plot for all participants together
hyr_data %>%
  group_by(condition,construction,rating) %>%
  summarise(count = n()) %>%
  mutate(percentage = count / sum(count) * 100) %>%
  ggplot(aes(x = factor(condition, 
                        levels = c("baseline", "island", "object"),
                        labels = c("no viol.", "island viol.", "A-min. viol.")),
             y = percentage, 
             fill = factor(rating, levels = 5:1))) +
  geom_bar(stat = "identity", color = "black") +
  facet_wrap(~factor(construction,
                     levels = c("hyperraising", "copy_raising"),
                     labels = c("Hyperraising", "Copy Raising"))) +
  labs(x = "Condition", y = "Proportion of responses", fill = "Rating") +
  scale_fill_manual(values = c("darkgreen", "green", "yellow", "orange", "red"), labels = score_labels) +
  theme_bw() +
  theme(axis.text.y = element_text(size = 14),
        axis.title.y = element_text(size = 14),
        axis.text.x = element_text(size=14, angle = 45, hjust = 1),
        axis.title.x = element_text(size=14),
        strip.text = element_text(size = 14)) -> figure1

# Saves the plot
ggsave("bar plot general.png", figure1, dpi = 600)

# Converts raiser_status into a factor variable
hyr_data$raiser_status <- factor(hyr_data$raiser_status,
                          levels = c("raiser", "nonraiser"),
                          labels = c("Raisers", "Non-raisers")) 

# Converts construction into a factor variable
hyr_data$construction <- factor(hyr_data$construction,
                                        levels = c("hyperraising", "copy_raising"),
                                        labels = c("Hyperraising", "Copy Raising"))

# Creates stacked bar plot with raiser_status as a faceting category
hyr_data %>%
  group_by(condition,construction,rating,raiser_status) %>%
  summarise(count = n()) %>%
  group_by(condition, construction, raiser_status) %>%
  mutate(total = sum(count),percentage = count / total * 100) %>%
  ggplot(aes(x = factor(condition, 
                        levels = c("baseline", "island", "object"),
                        labels = c("no viol.", "island viol.", "A-min. viol.")),,
             y = percentage, 
             fill = factor(rating, levels = 5:1))) +
  geom_bar(stat = "identity", color = "black") +
  facet_grid(raiser_status ~ construction) +
  labs(x = "Condition", y = "Proportion of responses", fill = "Rating") +
  scale_fill_manual(values = c("darkgreen", "green", "yellow", "orange", "red"), labels = score_labels) +
  theme_bw() +
  theme(axis.text.y = element_text(size = 12),
        axis.title.y = element_text(size = 12),
        axis.text.x = element_text(size=12, angle = 45, hjust = 1),
        axis.title.x = element_text(size=12),
        strip.text = element_text(size = 14)) -> subgroup_plot

# Saves the plot
ggsave("subgroup plot.png", subgroup_plot, dpi = 600, width = 6, height = 6)


# Delta rating plot

# Calculates delta_island and restructures the dataset
delta_rating <- hyr_data %>%
  filter(raiser_status == "raiser") %>%
  pivot_wider(id_cols = c(participant, construction),
              names_from = condition,
              values_from = rating) %>%
  mutate(delta_island = baseline - island,
         delta_object = baseline - object) %>%
  pivot_longer(cols = c(delta_island, delta_object),
               names_to = "condition_new",
               values_to = "delta_rating") %>%
  mutate(condition_new = gsub("delta_", "", condition_new))

# Creates the plot
delta_rating %>%
  ggplot(aes(x = factor(construction,
                        levels = c("hyperraising", "copy_raising"),
                        labels = c("Hyperraising", "Copy Raising")), 
             y = delta_rating, 
             color = factor(construction,
                           levels = c("hyperraising", "copy_raising"),
                           labels = c("Hyperraising", "Copy Raising")))) +
  geom_jitter(alpha = 0.3, size = 1.5, width = 0.2, shape = 16) +
  geom_violin(trim = TRUE, alpha=1,width=.75,linewidth=.75) +
  stat_summary(fun = median, geom = "point", 
               shape = 95, size = 15, color = "black", 
               position = position_nudge(y = 0.05)) +
  facet_grid(~factor(condition_new,
                     levels = c("island", "object"),
                     labels = c("Island-violating", "A-minimality-violating"))) +
  theme_bw(base_size = 16) +
  scale_color_manual(values = c("#E69F00", "#56B4E9")) +
  labs(y="ΔRating",color="Construction") +
  theme(axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        legend.position = "bottom") -> plot_delta

# Saves the plot
ggsave("delta plot.png", plot_delta, dpi = 600)
